package com.horizon.manage.classes.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.horizon.common.constrants.HorizonResultCodeEnum;
import com.horizon.common.util.HorizonUtil;
import com.horizon.manage.classes.converter.ClassesEvdConverter;
import com.horizon.manage.classes.dto.ClassesDTO;
import com.horizon.manage.classes.entity.Classes;
import com.horizon.manage.classes.sevice.IClassesService;
import com.horizon.manage.classes.vo.ClassesVO;
import com.horizon.manage.item.dto.ItemDTO;
import com.horizon.manage.item.entity.Item;
import com.horizon.manage.item.vo.ItemVO;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.api.ResultCode;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Map;

/**
 * 班级controller.
 *
 * @author zhaoshb
 * @since 2022/12/8 22:57
 */
@RestController
@AllArgsConstructor
@RequestMapping("/horizon/classes")
public class ClassesController {

    private IClassesService classesService;

    private ClassesEvdConverter classesEvdConverter;

    @PostMapping("/create")
    @ApiOperation(value = "创建", notes = "创建")
    public R<Long> create(@RequestBody ClassesDTO classesDTO) {
        Classes classes = classesEvdConverter.dtoToEntity(classesDTO);
        return classesService.save(classes) ? R.data(classes.getId()) : R.fail(ResultCode.FAILURE);
    }

    @PostMapping("/delete")
    @ApiOperation(value = "删除", notes = "删除")
    public R<Object> delete(@RequestBody Long classesId) {
        int rowAffected = classesService.getBaseMapper().deleteById(classesId);
        return rowAffected == 1 ? R.data(null) : R.fail(HorizonUtil.getLocaleMessage(HorizonResultCodeEnum.CLASSES_NOT_EXIST));
    }

    @PostMapping("/update")
    @ApiOperation(value = "更新", notes = "更新")
    public R<Object> update(@RequestBody ClassesDTO dto) {
        Classes classes = classesEvdConverter.dtoToEntity(dto);
        return R.status(classesService.updateById(classes));
    }

    @GetMapping("/list")
    @ApiOperation(value = "分页", notes = "传入cond以及排序条件")
    public R<IPage<ClassesVO>> list(@ApiIgnore @RequestParam Map<String, Object> cond, Query query) {
        IPage<Classes> pages = classesService.page(Condition.getPage(query), Condition.getQueryWrapper(cond, Classes.class));
        return R.data(classesEvdConverter.pageVO(pages));
    }
}
